
package biblio.service.category;


import biblio.dao.CategoryDAO;
import biblio.domain.Category;
import biblio.util.SourceName;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Provides data to editing and deleting.
 * 
 * @author mephisto
 */
@ManagedBean(name = "catList")
@ViewScoped
public class CategoryList implements Serializable
{
    public void remove()
    {
        Category tmp = allItems.get(currentCategoryIndex);
        categoryService.delete(tmp);
        allItems.remove(tmp);
    }

    public void store()
    {
        categoryService.update(editedCategory);
        allItems.set(currentCategoryIndex, editedCategory);
    }

    public List<Category> getAllItems()
    {
        synchronized (this)
        {
            if (allItems == null)
            {
                CategoryDAO categoryDAO = (CategoryDAO)appContext.getBean("catDAO");
                allItems = categoryDAO.selectAll();
            }
        }
        return allItems;
    }

    public int getCurrentCategoryIndex()
    {
        return currentCategoryIndex;
    }

    public void setCurrentCategoryIndex(int currentCategoryIndex)
    {
        this.currentCategoryIndex = currentCategoryIndex;
    }

    public Category getEditedCategory()
    {
        return editedCategory;
    }

    public void setEditedCategory(Category editedCategory)
    {
        this.editedCategory = editedCategory;
    }

    public int getPage()
    {
        return page;
    }

    public void setPage(int page)
    {
        this.page = page;
    }

    public void setCategoryService(CategoryServiceImpl categoryService)
    {
        this.categoryService = categoryService;
    }

    static
    {
        appContext = new ClassPathXmlApplicationContext( new String[] {
                SourceName.DAO_APPLICATION_CONTEXT
        });
    }


    private static final long serialVersionUID = -3832235132261771583L;
    private static ApplicationContext appContext;
    private static Logger log = Logger.getLogger(CategoryList.class);
    private int page = 1;
    private List<Category> allItems = null;
    private int currentCategoryIndex;
    private Category editedCategory;
    @ManagedProperty(value="#{catService}")
    private CategoryServiceImpl categoryService;
}
